package com.agp.controller.admin;

import com.agp.dto.HealthRecordDTO;
import com.agp.dto.MessageQueryDTO;
import com.agp.dto.TreeDTO;
import com.agp.dto.TreeUpdateDTO;
import com.agp.entity.HealthRecord;
import com.agp.result.Result;
import com.agp.service.TreeMediaService;
import com.agp.service.TreeService;
import com.agp.utils.AliOssUtil;
import com.agp.vo.MessageVO;
import com.agp.vo.TreeDetailVO;
import com.agp.vo.TreeFamilyCountVO;
import com.agp.vo.TreeGenusCountVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/admin/tree")
@Api(tags = "古树管理")
@Slf4j
public class TreeController {
    @Autowired
    private TreeService treeService;
    @Autowired
    private TreeMediaService treeMediaService;
    @Autowired
    private AliOssUtil aliOssUtil;


    /**
     * 新增古树信息
     *
     * @param treeDTO
     * @return
     */
    // 新增古树信息
    @PostMapping
    @ApiOperation("新增古树信息")
    public Result<Integer> save(@RequestBody @Validated TreeDTO treeDTO) throws Exception {
        log.info("新增古树信息{}", treeDTO);
        Integer treeId = treeService.save(treeDTO);
        log.info("保存成功，treeId为{}", treeId);
        return Result.success(treeId);
    }

    // 删除功能，实现批量删除和单个删除两种方式
    @Transactional
    @DeleteMapping
    @ApiOperation("批量删除古树信息")
    public Result delete(Integer[] ids) {
        log.info("批量删除古树信息:{}", Arrays.toString(ids));
        //  批量删除古树信息
         treeService.delete(ids);
        // 删除树木图片信息
         treeMediaService.deleteByTreeId(ids);
        return Result.success();
    }

    // 更新功古树信息
    @PutMapping
    @ApiOperation("更新古树信息")
    public Result update(@RequestBody @Validated TreeUpdateDTO treeUpdateDTO) {
        log.info("动态更新古树信息:{}", treeUpdateDTO);
        treeService.update(treeUpdateDTO);
        return Result.success();
    }

    /**
     * 获取古树数量统计信息
     *
     * @return
     */
    @GetMapping("/familycount")
    @ApiOperation("获取古树科名数量统计信息")
    public Result<List<TreeFamilyCountVO>> getTreeFamilyCount() {
        List<TreeFamilyCountVO> treeFamilyCountVOList = treeService.getTreeFamilyCount();
        log.info("科名比例统计：{}",  treeFamilyCountVOList);
        return Result.success(treeFamilyCountVOList);
    }

    /**
     * 获取古树属名数量统计信息
     *
     * @return
     */
    @GetMapping("/genuscount")
    @ApiOperation("获取古树科名数量统计信息")
    public Result<List<TreeGenusCountVO>> getTreeGenusCount() {
        List<TreeGenusCountVO> treeFamilyCountVOList = treeService.getTreeGenusCount();
        log.info("属名比例统计：{}",  treeFamilyCountVOList);
        return Result.success(treeFamilyCountVOList);
    }


    /**
     * 获取古树详情信息
     *
     * @param treeId
     * @return
     */
    @GetMapping("/{treeId}")
    @ApiOperation("获取古树详情信息")
    public Result<TreeDetailVO> getDetailTreeInfo(@PathVariable Integer treeId) {
        log.info("获取古树详细信息，id：{}", treeId);
        TreeDetailVO treeDetailVO = treeService.getDetailTreeInfo(treeId);
        return Result.success(treeDetailVO);
    }


    //上传不健康的古树图片
    @PostMapping("/uploadUnhealthyTreeImage")
    @ApiOperation("上传不健康古树图片")
    public Result<List<String>> uploadUnhealthyTreeImage(@RequestParam("file") MultipartFile[] files) throws Exception {
        log.info("上传不健康古树图片");
        List<String> urls = aliOssUtil.uploadUnhealthyTreeImage(files);
        return Result.success(urls);
    }

    @PostMapping("/healthRecord")
    @ApiOperation("添加健康记录")
    public Result addHealthyRecord(@RequestBody HealthRecordDTO healthRecordDTO) throws JsonProcessingException {
        log.info(" 接收到的photoUrls实际类型：{}", healthRecordDTO.getPhotoUrls().getClass().getName());
        treeService.addHealthyRecord(healthRecordDTO);
        return Result.success();
    }

    /**
     * 获取古树健康信息
     *
     * @param messageQueryDTO
     * @return
     */
    @PostMapping("/detialMessage")
    @ApiOperation("获取古树健康信息")
    public Result<List<MessageVO>> getHealthyRecord(@RequestBody MessageQueryDTO messageQueryDTO ) {
        log.info("获取古树健康信息");
        List<MessageVO> messageVOList = treeService.getHealthyRecord(messageQueryDTO);
        return Result.success(messageVOList);
    }

    @PutMapping("/updateHealthRecordStatus")
    @ApiOperation("更新健康记录状态")
    public Result updateHealthRecordStatus(@RequestBody HealthRecord healthRecord) {
        log.info("更新健康记录状态:{}",  healthRecord);
        treeService.updateHealthRecordStatus(healthRecord);
        return Result.success();
    }


}
